﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.General.BL
{
    public class LoginManager
    {
        public string Login(Session sysSess, string stUser, string stPassword, string stCompany)
        {
            string stSQL = "USR_Login";
            DataHelper helper = null;
            string stErr = null;

            try
            {
                helper = new DataHelper(sysSess);
                return helper.GetString(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                            DataHelper.BuildParameter(sysSess, "@USER_ID", stUser),
                            DataHelper.BuildParameter(sysSess, "@PASSWORD", stPassword),
                            DataHelper.BuildParameter(sysSess, "@COMPANY", stCompany)});
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetEnvironmentList(Session sysSess, int iUserNum)
        {
            string stSQL = "ENV_RAuthEnvironments";
            DataHelper helper = null;
            DataTable dt = null;

            try
            {
                helper = new DataHelper(sysSess);
                dt = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                            DataHelper.BuildParameter(sysSess, "@INTERNAL_USER_NUM", iUserNum)});
                return dt;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string GetEnvConnString(Session sysSess, int iEnvNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sysSess);
                return helper.GetString(CommandType.Text, "SELECT CONN_STRING FROM ENVIRONMENT WHERE INTERNAL_ENV_NUM = @INTERNAL_ENV_NUM", new IDataParameter[]{
                            DataHelper.BuildParameter(sysSess, "@INTERNAL_ENV_NUM", iEnvNum)});
            }
            catch(Exception e){
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string ValidateLicense()
        {
            LicenseKeyValidation licxMgr = null;
            licxMgr = new LicenseKeyValidation();
            return licxMgr.ValidateLicense(null);
        }
    }
}
